<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/8
 * Time: 13:46
 */
namespace app\blog\controller;
use cmf\controller\AdminBaseController;
use think\Db;


class FormatController extends AdminBaseController
{
    //商品列表页面
    public function index()
    {
        $pid = $this->request->param("pid");
        $data=Db::name('Format')->where(array('pid'=>$pid))->select();
        $this->assign("data", $data);
        $this->assign("pid", $pid);
        return $this->fetch();
    }
    //新增
    public function add()
    {
        $pid = $this->request->param("pid");
        $store_type_list = Db::name('StoreType')->field('id,cat_name')->select()->toarray();
        $this->assign("store_type_list", $store_type_list);
        $this->assign("pid", $pid);
        return $this->fetch();
    }
    public function addPost(){
        if ($this->request->isPost()){
            $res  = $this->request->param();
            //dump($res);exit;
            $data['store_type']=$res['type'];
            $data['format']=$res['options']['format'];
            $data['price']=$res['options']['price'];
            $data['vipprice']=$res['options']['vipprice'];
            $data['pid']=$res['pid'];
            $data['ctime']=time();
            $re=Db::name('Format')->insert($data);
            if($re){
                $this->success("添加成功！", url("Format/index",array('pid'=>$res['pid'])));
            }else{
                $this->error("添加失败，请重新添加！", url("Format/add",array('pid'=>$res['pid'])));
            }
        }
    } 
    //编辑
    public function edit()
    {
        $id = $this->request->param("id");
        $store_type_list = Db::name('StoreType')->field('id,cat_name')->select()->toarray();       
        $data=Db::name('Format')->where(array('id'=>$id))->find();
        $store_type_list = Db::name('StoreType')->field('id,cat_name')->select()->toarray();
        $this->assign("store_type_list", $store_type_list);
        $this->assign("data", $data);
        return $this->fetch();
    } 
    public function editPost(){
        if ($this->request->isPost()){
            $res  = $this->request->param();          
            $data['store_type']=$res['type'];
            $data['format']=$res['options']['format'];
            $data['price']=$res['options']['price'];
            $data['vipprice']=$res['options']['vipprice'];
            $data['pid']=$res['pid'];
            $data['ctime']=time();
            $re=Db::name('Format')->where(array('id'=>$res['id']))->update($data);
            if($re){
                $this->success("编辑成功！", url("Format/index",array('pid'=>$res['pid'])));
            }else{
                $this->error("编辑失败，请重新编辑！");
            }
        }
    }
}